import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:shenling_wuliu/mine/mine.dart';
import 'package:shenling_wuliu/tab_bar/task.dart';
import 'package:shenling_wuliu/utils/eventbus.dart';

class TabBarWidget extends StatefulWidget {
  const TabBarWidget({super.key});

  @override
  State<TabBarWidget> createState() => _TabBarWidget();
}

class _TabBarWidget extends State<TabBarWidget> {
  final Color customColor = const Color(0xFFEF4F3F);
   int currentIndex = 0;
  // 底部TabBar数据
  List tabBarList = [
    {
      "title": "任务",
      "default": "ic_tab_btn_task.svg",
      "active": "ic_tab_btn_task.svg"
    },
    {
      "title": "消息",
      "default": "ic_tab_btn_mess_nor.svg",
      "active": "ic_tab_btn_mess_nor.svg"
    },
    {
      "title": "我的",
      "default": "ic_tab_btn_mine_nor.svg",
      "active": "ic_tab_btn_mine_nor.svg"
    }
  ];
  @override
  void initState() {
    super.initState();
    
    // var token = TokenManager().getToken();
    // print(token);
    eventBus.on<LogoutEvent>().listen((event){
      Navigator.pushNamed(context, '/');
    });
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(child: IndexedStack(
        index: currentIndex,
        children: [
          TaskWidget(),
          Text('消息'),
          MineWidget(currentIndex: currentIndex,),
        ],
      )),
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: currentIndex,
        items: getBottomBarList(),
        selectedItemColor: customColor,
        onTap: (index) {
          setState(() {
            currentIndex = index;
          });
        },
      ),
    );
  }
  List<BottomNavigationBarItem> getBottomBarList() {
    List<BottomNavigationBarItem> list = [];
    for (var item in tabBarList) {
      list.add(BottomNavigationBarItem(
        icon: SvgPicture.asset(item['default'],width: 25,),
        activeIcon: SvgPicture.asset(item['active'],width: 25,colorFilter: ColorFilter.mode(customColor, BlendMode.srcIn),),
        label: item['title'],
      ));
    }
    return list;
  }
}